﻿using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.Database;
using Android.Database.Sqlite;
using Android.Util;

namespace KnowledgeInput
{
    [Activity(Label = "Textbook")]
    public class Textbook : Activity
    {
        string tag = "Textbook";
        string id = "";
        string db_path = "";
        EditText tip_edit_text = null;
        EditText answer_edit_text = null;
        Button toggle_show_answer_button = null;
        EditText input_edit_text = null;
        Button check_button = null;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Create your application here
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            this.SetContentView(Resource.Layout.textbook);

            tip_edit_text = FindViewById<EditText>(Resource.Id.tip_edit_text);
            answer_edit_text = FindViewById<EditText>(Resource.Id.answer_edit_text);
            answer_edit_text.Visibility = ViewStates.Invisible;
            toggle_show_answer_button = FindViewById<Button>(Resource.Id.toggle_show_answer_button);
            toggle_show_answer_button.Click += OnToggleShowAnswer;
            input_edit_text = FindViewById<EditText>(Resource.Id.input_edit_text);
            check_button = FindViewById<Button>(Resource.Id.check_button);
            check_button.Click += OnCheck;

            this.id = Intent.GetStringExtra("id");
            this.db_path = Intent.GetStringExtra("db_path");

            SQLite sql = new SQLite(this, db_path);
            SQLiteDatabase db = sql.ReadableDatabase;
            string[] args = new string[] { id, id };
            ICursor cursor = db.RawQuery("SELECT `node_name`, `content` " +
                "FROM `node` " +
                "WHERE (`parent_node_id` = ? OR `id` = ?) AND `content` <> '' " +
                "ORDER BY RANDOM() " +
                "LIMIT 1", args);
            Log.Info(tag, "cursor count: " + cursor.Count.ToString());
            if (cursor.MoveToFirst())
            {
                int node_name_index = cursor.GetColumnIndex("node_name");
                int content_index = cursor.GetColumnIndex("content");
                tip_edit_text.Text = cursor.GetString(node_name_index);
                answer_edit_text.Text = cursor.GetString(content_index);
            }
        }
        private void OnToggleShowAnswer(object sender, EventArgs e)
        {
            if (answer_edit_text.Visibility != ViewStates.Visible)
            {
                answer_edit_text.Visibility = ViewStates.Visible;
            }
            else
            {
                answer_edit_text.Visibility = ViewStates.Invisible;
            }
        }
        private void OnCheck(object sender, EventArgs e)
        {
            if (answer_edit_text.Text == input_edit_text.Text)
            {
                input_edit_text.Text = "";
                SQLite sql = new SQLite(this, db_path);
                SQLiteDatabase db = sql.ReadableDatabase;
                string[] args = new string[] { id, id };
                ICursor cursor = db.RawQuery("SELECT `node_name`, `content` " +
                    "FROM `node` " +
                    "WHERE (`parent_node_id` = ? OR `id` = ?) AND `content` <> '' " +
                    "ORDER BY RANDOM() " +
                    "LIMIT 1", args);
                if (cursor.MoveToFirst())
                {
                    int node_name_index = cursor.GetColumnIndex("node_name");
                    int content_index = cursor.GetColumnIndex("content");
                    tip_edit_text.Text = cursor.GetString(node_name_index);
                    answer_edit_text.Text = cursor.GetString(content_index);
                }
                AlertDialog.Builder dialog = new AlertDialog.Builder(this);
                dialog.SetTitle("正确");
                dialog.SetMessage("录入正确");
                dialog.Show();
            }
            else
            {
                AlertDialog.Builder dialog = new AlertDialog.Builder(this);
                dialog.SetTitle("错误");
                dialog.SetMessage("录入错误");
                dialog.Show();
            }
        }
    }
}